package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.JUsrPerm;
import com.miplus.ccrm.core.controller.vo.conf.WfActReqVo;
import com.miplus.ccrm.core.controller.vo.conf.WfActResVo;
import com.miplus.ccrm.core.pojo.WfActions;
import com.miplus.ccrm.core.pojo.WfCaseactions;
import com.miplus.ccrm.core.pojo.WfCases;
import com.miplus.ccrm.core.mapper.WfCasesMapper;
import com.miplus.ccrm.core.pojo.WfSteps;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【wf_cases】的数据库操作Service实现
* @createDate 2024-04-04 21:42:06
*/
@Service
public class WfCasesServiceImpl extends MPJBaseServiceImpl<WfCasesMapper, WfCases>
    implements WfCasesService{
    @Override
    public int wfCaseAdd(String $usrid,Integer wfid,Integer originalid)
    {
        //取消现在执行的工作流
        wfCaseCancel($usrid,wfid,originalid);

        WfCases wfCases=new WfCases();
        wfCases.setStatus("1");
        wfCases.setWfid(wfid);
        wfCases.setOriginalid(originalid);
        wfCases.setApplyusr($usrid);
        wfCases.setApplydt(new Date());
        baseMapper.insert(wfCases);

        return wfCases.getCaseid();
    }

    @Override
    public int wfCaseCancel(String $usrid,Integer wfid,Integer originalid)
    {
        WfCases wfCases=new WfCases();
        wfCases.setStatus("4");
        wfCases.setCancelusr($usrid);
        wfCases.setCanceldt(new Date());
        //把现在进行中的工作流取消掉
        return baseMapper.update(wfCases,
                new QueryWrapper<WfCases>()
                        .eq("wfid",wfid)
                        .eq("originalid",originalid)
                        .eq("status","1")
        );
    }

    @Override
    public List<WfActResVo> getActionList(JSysUser user,WfActReqVo wfActReqVo)
    {
        List<WfActResVo> voList=selectJoinList(WfActResVo.class,
                new MPJLambdaWrapper<WfCases>()
                        .selectAs("wa.actionid",WfActResVo::getId)
                        .selectAs("wa.name",WfActResVo::getName)
                        .selectAs("wca.caseactid",WfActResVo::getCaseactid)
                        .selectAs("wa.permossionid",WfActResVo::getPermossionid)
                        .leftJoin(WfCaseactions.class,"wca",WfCaseactions::getCaseid,WfCases::getCaseid)
                        .leftJoin(WfSteps.class,"ws",WfSteps::getStepid,WfCaseactions::getStepid)
                        .leftJoin(WfActions.class,"wa",WfActions::getStepid,WfCaseactions::getStepid)
                        .eq(WfCases::getOriginalid,wfActReqVo.getOriginalid())
                        .eq(WfCases::getWfid,wfActReqVo.getWfid())
                        .isNull(WfCaseactions::getActionid)
                        .eq(WfActions::getType,"1")
                        .eq(WfCases::getStatus,"1")
        );
        //如工作流动作权限为空，或动作权限关联用户下的权限
        List<WfActResVo> voList2=new ArrayList<>();
        if(JPubUtils.IsNotEmpty(voList)) {
            for (WfActResVo resVo : voList) {
                if (JPubUtils.IsNotEmpty(resVo.getPermossionid())) {
                    if (JUsrPerm.hasPermission(user, resVo.getPermossionid())) {
                        voList2.add(resVo);
                    }
                } else voList2.add(resVo);
            }
            voList.clear();
        }

        return voList2;
    }

    @Override
    public int wfCaseClose(String $usrid,Integer wfcaseid)
    {
        WfCases wfCases=new WfCases();
        //WF.CASE.STATUS，2=关闭
        wfCases.setCaseid(wfcaseid);
        wfCases.setStatus("2");
        wfCases.setCloseusr($usrid);
        wfCases.setClosedt(new Date());
        //把现在进行中的工作流取消掉
        return baseMapper.updateById(wfCases);
    }
}




